﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.CrossCutting.RepositoryPattern.Logging
{
    public class EFInterceptor : IDbCommandInterceptor
    {

        private IEFLogger _logger;

        public EFInterceptor(IEFLogger logger)
        {
            _logger = logger;
        }

        public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {

            _logger.LogInformation("Test debug");
            var stopWatch = (interceptionContext.UserState as System.Diagnostics.Stopwatch);
            if (stopWatch != null)
            {
                stopWatch.Stop();
                _logger.LogPerformance(command.CommandText, command.CommandType.ToString(),"", stopWatch.ElapsedMilliseconds);
            }

            LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            var stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            interceptionContext.UserState = stopWatch;
            LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
        {
            _logger.LogInformation("Test debug");
            var stopWatch = (interceptionContext.UserState as System.Diagnostics.Stopwatch);
            if (stopWatch != null)
            {
                stopWatch.Stop();
                _logger.LogPerformance(command.CommandText, command.CommandType.ToString(), "", stopWatch.ElapsedMilliseconds);
            }
            LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
        {
            var stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            interceptionContext.UserState = stopWatch;
            LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }


        private void LogInfo(string command, string commandText)
        {
            Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
        }

    }
}


